/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package air_hockey_v2.gui;

import air_hockey_v2.engine.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author cooler
 */
public class JogoPanel extends JPanel implements TimerListener {

    private GuiItens itens[];
    private GuiPontos score;
    private boolean pause = false;
    private Timer timer;
    public static boolean exit = false;

    public JogoPanel(int difi) {
        setBackground(Color.WHITE);
        this.setFocusable(true);

        Disco disco = new Disco();
        disco.setX(382);
        disco.setY(332);
        Campo campo = new Campo();
        campo.setX(0);
        campo.setY(150);
        Taco p1 = new Taco("/images/taco_red.png");
        p1.setX(100);
        p1.setY(327);
        Taco p2 = new TacoIA("/images/taco_blue.png", difi);
        p2.setX(664);
        p2.setY(327);

        itens = new GuiItens[4];
        itens[0] = new GuiCampo(this, campo);
        itens[1] = new GuiDisco(this, disco);
        itens[2] = new GuiTaco(this, p1);
        itens[3] = new GuiTaco(this, p2);

        score = new GuiPontos(this, itens[1].getItem().getPontos());

        this.addMouseMotionListener(new OuvinteMouse());
        this.addKeyListener(new OuvinteTeclado());

        timer = new Timer(this, 17);
        timer.start();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        itens[0].draw(g);
        itens[1].draw(g);
        itens[2].draw(g);
        itens[3].draw(g);
    }

    public void update(Timer timer) {
        if (!pause) {
            int x = 0, y = 0;
            score.draw();
            itens[1].getItem().move(x, y);
            itens[2].getItem().colisao(itens[1].getItem());
            itens[1].getItem().colisao(itens[1].getItem());

            itens[3].getItem().colisao(itens[1].getItem());

            if(!itens[3].getItem().isColisao(itens[3].getItem().getX() + 23, itens[3].getItem().getY() + 23, itens[1].getItem())){
                //Se nao colidiu com o disco, move o TacoIA
                itens[3].getItem().move(itens[1].getItem().getX(), itens[1].getItem().getY());
            }

            pontuacaoFinal(score.getPontos().pontuacaoFinal());
            repaint();
        }
    }

    public void pontuacaoFinal(boolean flag) {
        if (flag) {
            timer.setRun(false);
            MenuPrincipal.getSom().loop();
            JPanel panel = new JPanel(new GridLayout(2, 1));
            JLabel label = new JLabel("Nome:");
            JTextField nomeText = new JTextField();
            nomeText.setFocusable(true);
            panel.add(label);
            panel.add(nomeText);
            String nome = "";
            while ("".equals(nome)) {
                JOptionPane.showMessageDialog(this, panel, "Pontuação", JOptionPane.INFORMATION_MESSAGE);
                nome = nomeText.getText();
            }
            Pontuacoes pontuacao = new Pontuacoes();
            pontuacao.add(nome, score.getPontos().getVermelho(), score.getPontos().getAzul());
            exit = true;
        }
    }

    private class OuvinteMouse extends MouseMotionAdapter {

        @Override
        public void mouseMoved(MouseEvent e) {

            itens[2].getItem().setVy(1);
            itens[2].getItem().setVx(1);
            if (!pause && (e.getX() - 23) <= 50 || (e.getX() + 23) >= 400 || (e.getY() - 23) <= 175 || (e.getY() + 46) >= 550) {
                //MOUSE FORA DO CAMPO DE JOGO
            } else {
                MouseVelocidade mVel = new MouseVelocidade(e);
                mVel.start();
                if (!itens[2].getItem().isColisao(e.getX(), e.getY(), itens[1].getItem())) {
                    itens[2].getItem().move(e.getX(), e.getY());
                } else {
                    itens[2].getItem().melhorXY(e.getX(), e.getY(), itens[1].getItem());
                }
                itens[2].getItem().setVxM(mVel.getVx());
                itens[2].getItem().setVyM(mVel.getVy());
            }

        }
    }

    public boolean isPause() {
        return pause;
    }

    public void setPause(boolean pause) {
        this.pause = pause;
    }

    public boolean isExit() {
        return exit;
    }

    public Timer getTimer() {
        return timer;
    }

    private class OuvinteTeclado extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_P) {
                setPause(!isPause());
            }
        }
    }
}
